local assets = {
	Asset("ANIM", "anim/musha_armor_original.zip"),
	Asset("ATLAS", "images/inventoryimages/musha_armor_original.xml"),
	Asset("IMAGE", "images/inventoryimages/musha_armor_original.tex"),
	Asset("ANIM", "anim/musha_backpack3.zip")
}

local function Updata(inst, display)
	if inst.components.musha_equipment:IsBroken() then
		inst.components.armor:SetAbsorption(0)
		return
	end
	inst.components.armor:SetAbsorption(math.floor(50 + (inst.components.musha_equipment.currentlevel - 1) * 1.03) * 0.01)
	if display then
		inst.components.talker:Say(
			string.format(
				STRINGS.MUSHA_ARMOR_ORIGINAL .. " (LV%d)\n" .. STRINGS.MUSHA_ARMOR .. " (%d)",
				inst.components.musha_equipment.currentlevel,
				math.floor(50 + (inst.components.musha_equipment.currentlevel - 1) * 1.03)
			)
		)
	end
end

local function OnBlocked(owner)
	owner.SoundEmitter:PlaySound("dontstarve/wilson/hit_armour")
end

local function OnOpen(inst, owner)
	if inst.components.container ~= nil then
		inst.components.container:Open(owner)
	end
end

local function OnClose(inst, owner)
	if inst.components.container ~= nil then
		inst.components.container:Close(owner)
	end
end

local function OnEquip(inst, owner)
	inst:ListenForEvent("blocked", OnBlocked, owner)

	SpawnPrefab("green_leaves").Transform:SetPosition(inst:GetPosition():Get())

	Updata(inst)

	if inst.components.container ~= nil then
		inst.components.container:Open(owner)
	end

	owner.AnimState:OverrideSymbol("swap_body", "musha_armor_original", "swap_body")

	local switch = {
		["BMM"] = [[owner.AnimState:OverrideSymbol("swap_body_tall", "musha_backpack6", "swap_body_tall")]],
		["BT"] = [[owner.AnimState:OverrideSymbol("swap_body_tall", "musha_backpack4", "swap_body_tall")]],
		["BS"] = [[owner.AnimState:OverrideSymbol("swap_body_tall", "musha_backpack3", "swap_body_tall")]],
		["BM"] = [[owner.AnimState:OverrideSymbol("swap_body_tall", "musha_backpack", "swap_body_tall")]],
		["BL"] = [[owner.AnimState:OverrideSymbol("swap_body_tall", "musha_backpack2", "swap_body_tall")]],
		["WSP"] = [[owner.AnimState:OverrideSymbol("swap_body_tall", "musha_armor_forest_pink", "swap_body_tall")]],
		["WSR"] = [[owner.AnimState:OverrideSymbol("swap_body_tall", "musha_armor_forest_danger", "swap_body_tall")]],
		["WSB"] = [[owner.AnimState:OverrideSymbol("swap_body_tall", "musha_armor_forest_safe", "swap_body_tall")]],
		["WSH"] = [[owner.AnimState:OverrideSymbol("swap_body_tall", "musha_armor_forest", "swap_body_tall")]],
		["WLR"] = [[owner.AnimState:OverrideSymbol("swap_body_tall", "armor_butterfly", "swap_body_tall")]]
	}

	if inst.visual ~= "off" then
		loadstring(switch[string.upper(inst.visual)])
	end
end

local function OnUnequip(inst, owner)
	inst:RemoveEventCallback("blocked", OnBlocked, owner)
	owner.AnimState:ClearOverrideSymbol("swap_body")
	owner.AnimState:ClearOverrideSymbol("backpack")
	owner.AnimState:ClearOverrideSymbol("swap_body_tall")

	if inst.components.container ~= nil then
		inst.components.container:Close(owner)
	end
end

local function fn()
	local inst = CreateEntity()

	inst.entity:AddTransform()
	inst.entity:AddAnimState()
	inst.entity:AddSoundEmitter()
	inst.entity:AddNetwork()

	MakeInventoryPhysics(inst)
	MakeInventoryFloatable(inst, nil, nil)

	inst:AddComponent("talker")
	inst.components.talker.fontsize = 30
	inst.components.talker.font = TALKINGFONT
	inst.components.talker.colour = Vector3(0.9, 1, 0.75, 1)
	inst.components.talker.offset = Vector3(0, 150, 0)
	inst.components.talker.symbol = "swap_object"

	inst.AnimState:SetBank("armor_wood")
	inst.AnimState:SetBuild("musha_armor_original")
	inst.AnimState:PlayAnimation("anim")

	inst:AddTag("backpack")
	inst:AddTag("musha_items")

	inst.foleysound = "dontstarve/movement/foley/metalarmour"

	inst.entity:SetPristine()

	if TheNet:GetIsClient() then
		inst.OnEntityReplicated = function(inst)
			inst.replica.container:WidgetSetup("backpack")
		end
		return inst
	end

	inst:AddComponent("equippable")
	inst.components.equippable:SetOnEquip(OnEquip)
	inst.components.equippable:SetOnUnequip(OnUnequip)

	inst:AddComponent("musha_equipment")
	inst.components.musha_equipment:SetUpgradable(true)
	inst.components.musha_equipment:InitMushaEquip("armor", 450, 0)
	inst.components.musha_equipment:SetOnUpdataFn(Updata)
	inst.components.musha_equipment:SetOnBrokenFn(
		function()
			if inst.components.armor.absorb_percent ~= 0 then
				inst.components.musha_equipment:StopConsuming()
				inst.components.armor:SetAbsorption(0)
				inst.components.talker:Say(
					STRINGS.MUSHA_ARMOR_BROKEN .. "\n" .. STRINGS.MUSHA_ARMOR .. " (0)\n" .. STRINGS.MUSHA_ITEM_DUR .. " (0)"
				)
			end
			inst.components.musha_equipment:StopConsuming()
		end
	)

	inst.components.inventoryitem.atlasname = "images/inventoryimages/musha_armor_original.xml"

	inst:AddComponent("container")
	inst.components.container:WidgetSetup("backpack")
	-- inst.components.container.onopenfn = OnOpen
	-- inst.components.container.onclosefn = OnClose

	inst.type = "none"

	return inst
end

return Prefab("musha_armor_original", fn, assets)
